/**   
 * Copyright © 2018 HIT akeil. Tech Ltd. All rights reserved.
 * 
 * 功能描述：
 * @Package: com.akeil.ui.dialogs 
 * @author: snow   
 * @date: 2018年6月27日 上午8:19:59 
 */
package com.akeil.flash.dialog;

import java.net.SocketException;
import java.net.UnknownHostException;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.yang.serialport.utils.AppMD5Util;
import com.yang.serialport.utils.ByteUtils;
import com.yang.serialport.utils.IpUtil;
import com.yang.serialport.utils.SoftwareGrantControl;

/**
 * Copyright: Copyright (c) 2018 HIT akeil
 * 
 * @ClassName: LicenseDialog.java
 * @Description: 该类的功能描述
 *
 * @version: v1.0.0
 * @author: snow
 * @date: 2018年6月27日 上午8:19:59
 *
 *        Modification History: Date Author Version Description
 *        ---------------------------------------------------------* 2018年6月27日
 *        snow v1.0.0 修改原因
 */
public class LicenseDialog extends TitleAreaDialog {
	@Override
	protected void configureShell(Shell newShell) {
		// TODO 自动生成的方法存根
		super.configureShell(newShell);
		newShell.setText("授权管理");
	}

	private Composite pane_parent = null;
	
	private Text text_remark;
	private Text text_name;

	public LicenseDialog(Shell parentShell) {
		super(parentShell);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		// TODO Auto-generated method stub
		Composite composite = (Composite) super.createDialogArea(parent);
		// 自定义要创建的内容
		
		createContentPane(composite);
		return composite;
	}
	
	private void createContentPane(Composite parent) {
		Composite composite = new Composite(parent, SWT.NULL);
		//gd_composite.widthHint = 174;
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		// 采用三列的GridLayout
		Label label_remark;
		
		composite.setLayout(new FormLayout());

		final Label label_name = new Label(composite, SWT.NONE);
		final FormData formData = new FormData();
		formData.top = new FormAttachment(0, 27);
		formData.left = new FormAttachment(0, 30);
		formData.right = new FormAttachment(0, 60);
		label_name.setLayoutData(formData);
		label_name.setText("编号");

		text_name = new Text(composite, SWT.BORDER);
		text_name.setEditable(false);
		final FormData formData_1 = new FormData();
		formData_1.top = new FormAttachment(0, 25);
		formData_1.right = new FormAttachment(100, -32);
		formData_1.left = new FormAttachment(label_name, 5);
		text_name.setLayoutData(formData_1);
		text_name.setText(ByteUtils.intToHexString(Integer.parseInt(SoftwareGrantControl.getHardDiskSN("c")), true));
		/*String hd5 = "mac";
		try {
			hd5 = IpUtil.getLocalMac("127.0.0.1");
		} catch (SocketException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
		text_name.setText(hd5);*/
		
		text_remark = new Text(composite, SWT.BORDER);
		formData_1.bottom = new FormAttachment(text_remark, -24);
		text_remark.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				//cec110f7fd2deb32d36bb868239b0e3c
				checkValid();
			}
		});
		final FormData formData_2 = new FormData();
		formData_2.bottom = new FormAttachment(100, -10);
		formData_2.right = new FormAttachment(100, -32);
		formData_2.top = new FormAttachment(0, 75);
		formData_2.left = new FormAttachment(0, 65);
		text_remark.setLayoutData(formData_2);
		label_remark = new Label(composite, SWT.NONE);
		formData.bottom = new FormAttachment(label_remark, -25);
		final FormData formData_3 = new FormData();
		formData_3.bottom = new FormAttachment(100, -125);
		formData_3.top = new FormAttachment(0, 78);
		formData_3.right = new FormAttachment(label_name, 0, SWT.RIGHT);
		formData_3.left = new FormAttachment(0, 30);
		label_remark.setLayoutData(formData_3);
		label_remark.setText("密码");
		
		
		this.setTitle("请输入产品密钥");
		this.setMessage("请正确输入产品密钥",IMessageProvider.INFORMATION);
	}

	public Text getText_remark() {
		return text_remark;
	}

	public void setText_remark(Text text_remark) {
		this.text_remark = text_remark;
	}

	public Text getText_name() {
		return text_name;
	}

	public void setText_name(Text text_name) {
		this.text_name = text_name;
	}
	
	protected void checkValid(){
		String hd5 = AppMD5Util.getMD5(text_name.getText() + AppMD5Util.author + AppMD5Util.product);
		//String hd5 = "mac";
		try {
			//hd5 = AppMD5Util.getMD5(IpUtil.getLocalMac("127.0.0.1") + AppMD5Util.author + AppMD5Util.product);
			//hd5 = AppMD5Util.getMD5(IpUtil.getCPUID_Windows() + AppMD5Util.author + AppMD5Util.product);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} 
    	String password = text_remark.getText();
    	if(hd5.compareTo(password) != 0){
    		this.setErrorMessage("输入的密钥无效，请联系管理员后重新输入！");
    		this.getButton(IDialogConstants.OK_ID).setEnabled(false);
    	}else{
    		this.setErrorMessage(null);
    		this.getButton(IDialogConstants.OK_ID).setEnabled(true);
    	}
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		// TODO Auto-generated method stub
		super.createButtonsForButtonBar(parent);
		this.getButton(IDialogConstants.OK_ID).setEnabled(false);
	}
	

}
